package com.bicon.botu.finance.wallet.controller;

import com.bicon.botu.finance.wallet.common.FinanceConstants;
import com.bicon.botu.finance.wallet.common.WalletConstants;
import com.bicon.botu.finance.wallet.entity.Wallet;
import com.bicon.botu.finance.wallet.service.WalletDetailService;
import com.bicon.botu.core.base.common.Constants;
import com.bicon.botu.core.base.common.entity.BaseResult;
import com.bicon.botu.core.base.repository.redis.RedisHandler;
import com.bicon.botu.core.base.repository.redis.RedisTemplateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: 宋旭
 * @Date: Created in 19:07 2017/7/25/025
 */
@RestController
@RequestMapping("/V1.0.0/wallet")
public class WalletController {

    @Autowired
    private WalletDetailService walletDetailService;

    @Autowired
    private RedisHandler redisHandler;

    @RequestMapping(value = "/getDetailPage", method = RequestMethod.POST)
    public BaseResult getDetailPage(@RequestParam(value = "pageNo") int pageNo,
                                    @RequestParam(value = "pageSize") int pageSize,
                                    @RequestParam(value = "subjects") String subjects) {
        if (!(subjects.equals(FinanceConstants.WALLET_SUBJECTS_RECAHRGE)
                || subjects.equals(FinanceConstants.WALLET_SUBJECTS_WITHDRAW))) {
            return new BaseResult(Constants.RESPONSE_CODE_401, Constants.RESPONSE_MESSAGE_401);
        }
        return walletDetailService.selectWalletDetailPage(pageNo, pageSize, subjects);
    }


    /**
     * 初始化用户余额
     * @return
     */
    @RequestMapping(value = "/innitBalance",method = RequestMethod.POST)
    public BaseResult innitBalance(){

        RedisTemplateUtil redisTemplateUtil=redisHandler.getGlobalRedisTemplate();
        List<Wallet> list  = new Wallet().selectAll();
        for (Wallet wallet:list) {
            redisTemplateUtil.cacheStringValue(WalletConstants.BOTU_WALLET_BALANCE,wallet.getUserId(),String.valueOf(wallet.getMoney()));
        }
        return new BaseResult(WalletConstants.RESPONSE_CODE_200,WalletConstants.RESPONSE_MESSAGE_200);
    }
}
